Imports TeamUp.GarantiaTransacional
Imports Conexao
Imports System.Text

Public Class DaUsuario

    Inherits Model

    Public Sub New(ByVal objGarantiaTransacional As GTransacional)
        GarantiaTransacional = objGarantiaTransacional
    End Sub


    Public Function cadastrarUsuario(ByVal strNome As String, ByVal strLogin As String, ByVal strSenha As String) As Integer
        Dim strSql As String
        Dim objConBanco As ConexaoBanco = New ConexaoBanco
        Dim objResultado As Object
        Try
            strSql = "INSERT INTO usuario(nome,login,senha)VALUES('" + strNome + "','" + strLogin + "','" + strSenha + "') SELECT @@IDENTITY"
            objResultado = objConBanco.executarSPScalar(strSql, GarantiaTransacional.getConexao, GarantiaTransacional.getTransacao)

            If Not objResultado Is Nothing Then
                cadastrarUsuario = CInt(objResultado)
            Else
                cadastrarUsuario = -1
            End If

        Finally
        End Try
    End Function

    Public Function consultarUsuario(ByVal strCodigo As String) As Data.DataSet
        Dim strSql As StringBuilder
        Dim strCriterio As String = "WHERE"
        Dim objConBanco As ConexaoBanco = New ConexaoBanco
        Try
            strSql = New StringBuilder("SELECT nome, login, senha, codigo FROM usuario ")
            If strCodigo.Trim <> "" Then
                strSql.Append(strCriterio + " codigo = '" + strCodigo.Trim + "'")
            End If
            consultarUsuario = objConBanco.executarConsulta(strSql.ToString)
        Finally
        End Try
    End Function

    Public Sub alterarUsuario(ByVal strCodigo As String, ByVal strNome As String, ByVal strLogin As String, ByVal strSenha As String)
        Dim strSql As String
        Dim objConBanco As ConexaoBanco = New ConexaoBanco
        Dim objResultado As Object
        Try
            strSql = "UPDATE usuario set nome = '" + strNome + "',login = '" + strLogin + "',senha = '" + strSenha + "' WHERE codigo = " + strCodigo
            objResultado = objConBanco.executarSP(strSql, GarantiaTransacional.getConexao, GarantiaTransacional.getTransacao)
        Finally
        End Try
    End Sub

    Public Sub excluirUsuario(ByVal strCodigo As String)
        Dim strSql As String
        Dim objConBanco As ConexaoBanco = New ConexaoBanco
        Dim objResultado As Object
        Try
            strSql = "DELETE from usuario WHERE codigo = " + strCodigo
            objResultado = objConBanco.executarSP(strSql, GarantiaTransacional.getConexao, GarantiaTransacional.getTransacao)
        Finally
        End Try
    End Sub

End Class
